#!/usr/bin/python
# -*- coding: UTF-8 -*-
from flask import Blueprint, request, jsonify, json
from flask import render_template
from flask import g

from .compare import get_layui_json
from .db import get_db
from flaskr.CommonUtils.DBUtils import *
from flaskr.CommonUtils.CommonTool import *

import psycopg2
from psycopg2.extras import execute_values
from typing import List


bp = Blueprint("compareIndex", __name__)

@bp.route("/compareIndex")
def compare_index():
    task_name = ""
    id_string = ""
    source_column_info = ""
    if request.method == "GET":
        id_string = request.args.get("id")
        print("/detailCompareTask : id_string = " + str(id_string))
        dbutils = DBUtils()
        dbutils.value["dbname"] = "tzqdb"
        dbutils.value["username"] = "tzq"
        dbutils.value["password"] = "1"
        dbutils.value["host"] = "127.0.0.1"
        dbutils.value["port"] = "5432"
        print("str(id_string)=" + str(id_string))

        task_name = dbutils.exec_sql_get_result_from_gauss_or_pg("""
            select task_name from compare_task_t where task_id=""" + str(id_string) + """
            """)

        source_column_info = dbutils.exec_sql_get_result_from_gauss_or_pg("""
            select array_agg(distinct source) as arr,array_length(array_agg(distinct source), 1) as len
              from compare_task_info_v where task_id = """ + str(id_string) + """
            """)
        print("source_column_info = " + str(source_column_info))

    return render_template("compareIndex/compareIndex.html"
                           , source_column_info=source_column_info
                           , id=id_string, task_name=task_name)

@bp.route("/compareIndexData", methods=['GET'])
def compare_index_data():
    sql_string_tmp = ""
    table_name_tmp_string = ""
    index_name_tmp_string = ""
    check_result_tmp_string = ""
    if request.method == "GET":
        id_string = request.args.get("id")
        print("/compareIndexData : id_string = " + str(id_string))

        table_name_string = request.values.get("tablename")
        index_name_string = request.args.get("indexname")
        print("/compareIndexData : table_name = " + str(table_name_string))
        print("/compareIndexData : index_name = " + str(index_name_string))

        check_result_string = request.args.get("check_result")
        print("/compareIndexData : check_result_string = " + str(check_result_string))

        if table_name_string is None:
            table_name_string = ""
        if index_name_string is None:
            index_name_string = ""
        if table_name_string and len(table_name_string) != 0:
            table_name_tmp_string = table_name_tmp_string + " and tablename like '%" + table_name_string + "%'"
            print("/compareIndexData 2 1 : table_name_tmp_string = " + str(table_name_tmp_string))
        if index_name_string and len(index_name_string) != 0:
            index_name_tmp_string = index_name_tmp_string + " and indexname like '%" + index_name_string + "%'"
            print("/compareIndexData 2 2 : index_name_tmp_string = " + str(index_name_tmp_string))

        if check_result_string is None:
            check_result_string = ""
        if check_result_string and len(check_result_string) != 0:
            check_result_tmp_string = check_result_tmp_string + " and total_check_result = '" + check_result_string + "'"

        dbutils = DBUtils()
        dbutils.value["dbname"] = "tzqdb"
        dbutils.value["username"] = "tzq"
        dbutils.value["password"] = "1"
        dbutils.value["host"] = "127.0.0.1"
        dbutils.value["port"] = "5432"
        print("str(id_string)=" + str(id_string))
        sql_string_tmp = ("""
select * from get_index_info_by_task_id_f(""" + str(id_string) + """)
 where 1=1
               """ + str(table_name_tmp_string) + """
               """ + str(index_name_tmp_string) + """
               """ + str(check_result_tmp_string) + """
            """)
    return get_layui_json(sql_string_tmp)


